import pymysql

from pachong import leiji, shuju


def update_details():
    """
    更新details
    :return:
    """
    try:
        conn = pymysql.connect(host='localhost',
                               user='xxn',
                               password='123456',
                               db='cov',
                               charset='utf8'
                               )
        # 创建游标，默认元组
        cursor = conn.cursor()
        li = shuju()
        sql = 'insert into details(updata_T, provice, city, confirm, Confirm_add, dead, heal) values(' \
              '%s,%s,%s,%s,%s,%s,%s)'
        sql_query = 'select %s=(select updata_T from details order by id desc limit 1)'
        T = li[0][0]
        cursor.execute(sql_query, T)
        if not cursor.fetchone()[0]:
            print('start update')
            for item in li:
                cursor.execute(sql, item)
            conn.commit()
            print('ok')
        else:
            print('it is newest now')
    except:
        print('连接数据库失败！')
    finally:

        cursor.close()
        conn.close()
        print('数据库关闭成功！')


def update_leiji():
    """
    更新details
    :return:
    """
    try:
        conn = pymysql.connect(host='localhost',
                               user='xxn',
                               password='123456',
                               db='cov',
                               charset='utf8'
                               )
        # 创建游标，默认元组
        cursor = conn.cursor()
        li = leiji()
        sql = 'insert into leiji(nam, dat, nowConfirm, confirm, dead, heal) values(' \
              '%s,%s,%s,%s,%s,%s)'
        for item in li:
            cursor.execute(sql, item)
        conn.commit()
        print('ok')
    except:
        print('连接数据库失败！')
    finally:

        cursor.close()
        conn.close()
        print('数据库关闭成功！')


# update_details()
# update_leiji()

def update_country():
    """
    更新世界
    :return:
    """
    try:
        conn = pymysql.connect(host='localhost',
                               user='xxn',
                               password='123456',
                               db='cov',
                               charset='utf8'
                               )
        # 创建游标，默认元组
        cursor = conn.cursor()
        li = shuju()
        sql = 'insert into details(updata_T, provice, city, confirm, Confirm_add, dead, heal) values(' \
              '%s,%s,%s,%s,%s,%s,%s)'
        sql_query = 'select %s=(select updata_T from details order by id desc limit 1)'
        T = li[0][0]
        cursor.execute(sql_query, T)
        if not cursor.fetchone()[0]:
            print('start update')
            for item in li:
                cursor.execute(sql, item)
            conn.commit()
            print('ok')
        else:
            print('it is newest now')
    except:
        print('连接数据库失败！')
    finally:

        cursor.close()
        conn.close()
        print('数据库关闭成功！')


def update_das(i):
    """
    更新点赞
    :return:
    """
    try:
        conn = pymysql.connect(host='localhost',
                               user='zyh',
                               password='123456',
                               db='cov',
                               charset='utf8'
                               )
        # 创建游标，默认元组
        cursor = conn.cursor()
        sql = "UPDATE world SET dasnum= {} WHERE id=1;".format(i + 1)
        cursor.execute(sql)
        conn.commit()
        print('ok')
    except:
        print('连接数据库失败！')
    finally:
        cursor.close()
        conn.close()
        print('数据库关闭成功！')
